/**
 * 获取配置文件变量名
 * @param env
 */
import { GlobEnvConfig } from '@/./../types/config';

const getConfigFileName = (env: Record<any, string>) => {
  return `__PRODUCTION__${env.VITE || '__APP'}__CONF__`.toUpperCase().replace(/\s/g, '');
};

// 环境变量
export const getAppEnvConfig = () => {
  const ENV_NAME = getConfigFileName(import.meta.env);
  const ENV = import.meta.env.DEV ? (import.meta.env as unknown as GlobEnvConfig) : window[ENV_NAME as any];

  // 处理env的数据
  Object.keys(ENV).forEach((key) => {
    const it = ENV[key];
    // 判断是否是枚举值
    if (it === 'true' || it === 'false') {
      ENV[key] = it === 'true';
    }
  });

  const { VITE_APP_PUBLIC_PATH, VITE_APP_TITLE, VITE_APP_SHORT_NAME, VITE_APP_CAPITAL_API_URL, VITE_APP_API_URL_PREFIX } = ENV;

  // 设置全局配置
  return {
    baseUrl: VITE_APP_PUBLIC_PATH,
    title: VITE_APP_TITLE,
    shortName: VITE_APP_SHORT_NAME,
    apiUrl: VITE_APP_CAPITAL_API_URL,
    urlPrefix: VITE_APP_API_URL_PREFIX,
  };
};

export const APP_ENV_CONFIG = getAppEnvConfig();
